package com.scrx.ymg.action;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.google.gson.Gson;
import com.scrx.ymg.entity.AgentDrawings;
import com.scrx.ymg.entity.Banner;
import com.scrx.ymg.entity.DirectInfo;
import com.scrx.ymg.entity.DirectMoney;
import com.scrx.ymg.entity.DirectProduct;
import com.scrx.ymg.entity.OrderForm;
import com.scrx.ymg.entity.OrderFormDetails;
import com.scrx.ymg.entity.ProductEvaluate;
import com.scrx.ymg.entity.SuperMarketEntrepot;
import com.scrx.ymg.service.direct.IDirectService;
import com.scrx.ymg.service.entrepot.ISuperMarketEntrepotService;
import com.scrx.ymg.service.orderform.IOrderFormService;
import com.scrx.ymg.service.supermarket.ISuperMarketService;

@Controller
@RequestMapping("/directAction")
public class DirectAction {
	@Autowired
	private ISuperMarketService sms;
	@Autowired
	private IOrderFormService  ofs;
	@Autowired
	private IDirectService  direct;
	@Autowired
	private ISuperMarketEntrepotService smes;
	
	
    /**
     * 创建直营商家并创建商家资金账号
	 * @param info 商家信息 
	 * info.status:0.正常营业,1.冻结
     * @param licenseFile 营业执照图片
     * @param identityCardFile 法人身份证图片
     * @param brandFile 商标专利图片
     * @param qsImgFile QS标示图片
     * @param accreditFile 官方授权证书
	 * @return true: 创建成功,  false:创建失败,  251:账号或商家名称重复
     */
	@ResponseBody
	@RequestMapping(value ="/createDirectInfo",produces = "text/json;charset=UTF-8")
	public String createDirectInfo(DirectInfo info,MultipartFile licenseFile,MultipartFile identityCardFile
			,MultipartFile brandFile,MultipartFile qsImgFile,MultipartFile accreditFile){
		return direct.insertDirectInfo(info,licenseFile,identityCardFile,brandFile,qsImgFile,accreditFile);
	}
	/**
	 * 修改直营商家
	 * @param info
	 * @param licenseFile
	 * @param identityCardFile
	 * @param brandFile
	 * @param qsImgFile
	 * @param accreditFile
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/updateDirectInfoAll",produces = "text/json;charset=UTF-8")
	public String updateDirectInfoAll(DirectInfo info,MultipartFile licenseFile,MultipartFile identityCardFile
			,MultipartFile brandFile,MultipartFile qsImgFile,MultipartFile accreditFile){
		return direct.updateDirectInfoAll(info,licenseFile,identityCardFile,brandFile,qsImgFile,accreditFile);
	}
	
	/**
	 * 冻结恢复直营商家/修改商家信息
	 * @param info
	 * info.id:直营商家id
	 * info.status:0.正常营业,1.冻结
	 * @return true: 创建成功,  false:创建失败
	 */
	@ResponseBody
	@RequestMapping(value ="/updateDirectInfo",produces = "text/json;charset=UTF-8")
	public String updateDirectInfo(DirectInfo info){
		return direct.updateDirectInfo(info);
	}
	
    /**
     * 查询直营商家
     * @param info 各种条件
     * @param pageNum 第几页
     * @param pageCount 每页显示多少条
     * @return
     */
	@ResponseBody
	@RequestMapping(value ="/getDirectInfo",produces = "text/json;charset=UTF-8")
	public String getDirectInfo(DirectInfo info,int pageNum,int pageCount){
		Map<Object, Object> map=direct.getDirectInfo(info,pageNum,pageCount);
		Gson gson = new Gson();
		return gson.toJson(map);
	}
	
	/**
	 * 直营商家修改密码
	 * @param info
	 * info.account 账号
	 * passWord 密码;
	 * @param newPassWord 新密码
	 * @return 251 :账号或密码错误 true: 成功,  false:失败
	 */
	@ResponseBody
	@RequestMapping(value ="/updateDirectInfoPassword",produces = "text/json;charset=UTF-8")
	public  String updateDirectInfoPassword(DirectInfo info,String newPassWord){
		return direct.updateDirectInfoPassword(info,newPassWord);
	}
	   
	
    /**
     * 录入直营商品
     * @param product 商品信息
     * product.type:商品是否推销,0.普通商品,1推销商品
     * product.putaway:商品是否上架,0.已下架.1.已上架
	 * product.status:商品状态,-1.删除0.未审核.1审核通过,2审核失败
     * product.classify:商品所属分类.1.地方特色,2手机电器.3.精品生活
     * @param adFile  广告图片
     * @param thumbnailFile 缩略图
     * @param detailsFile   详情图
     * @param introduce1File 介绍图1
     * @param introduce2File 介绍图2
     * @param introduce3File 介绍图3
	 * @return true: 创建成功,  false:创建失败,  251:商品名称重复
     */
	@ResponseBody
	@RequestMapping(value ="/insertDirectProduct",produces = "text/json;charset=UTF-8")
	public String insertDirectProduct(DirectProduct product,MultipartFile adFile,
			MultipartFile thumbnailFile,MultipartFile detailsFile,MultipartFile introduce1File
			,MultipartFile introduce2File,MultipartFile introduce3File){

		return direct.insertDirectProduct(product,adFile,
				 thumbnailFile,detailsFile,introduce1File,introduce2File,introduce3File);
	}
	
	/**
	 * 更新直营商品//审核,修改,上架
     * @param product 商品信息
     * product.type:商品是否推销,0.普通商品,1推销商品
     * product.putaway:商品是否上架,0.已下架.1.已上架
	 * product.status:商品状态,-1.删除0.未审核.1审核通过,2审核失败
     * product.classify:商品所属分类.1.地方特色,2手机电器.3.精品生活
     * @param adFile  广告图片
     * @param thumbnailFile 缩略图
     * @param detailsFile   详情图
     * @param introduce1File 介绍图1
     * @param introduce2File 介绍图2
     * @param introduce3File 介绍图3
	 * @return true: 创建成功,  false:创建失败,  251:商品名称重复
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/updateDirectProduct",produces = "text/json;charset=UTF-8")
	public String updateDirectProduct(DirectProduct product,MultipartFile adFile,
			MultipartFile thumbnailFile,MultipartFile detailsFile,MultipartFile introduce1File
			,MultipartFile introduce2File,MultipartFile introduce3File){
		
		return direct.updateDirectProduct(product,adFile,
				 thumbnailFile,detailsFile,introduce1File,introduce2File,introduce3File);
	}
	
	/**
	 * 审核直营商品
	 * @param product
	 * product.status:商品状态,-1.删除0.未审核.1审核通过,2审核失败
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/checkDirectProduct",produces = "text/json;charset=UTF-8")
	public String checkDirectProduct(DirectProduct product){
		return direct.checkDirectProduct(product);
	}
	
    /**
     * 查询直营产品
     * @param product 各种条件
     * @param pageNum 第几页
     * @param pageCount 每页显示多少条
     * @return
     */
	@ResponseBody
	@RequestMapping(value ="/getDirectProduct",produces = "text/json;charset=UTF-8")
	public String getDirectProduct(DirectProduct product,int pageNum,int pageCount){
		Map<Object, Object> map=direct.getDirectProduct(product,pageNum,pageCount);
		Gson gson = new Gson();
		return gson.toJson(map);
	}
	
	/**
	 * 直营商家退货
	 * @param orderFormId 订单id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/directReturn",produces = "text/json;charset=UTF-8")
	public String directReturn(String orderFormId){
		return null;
	}
	
	/**
     * 查询直营商家营业额
     * @param info
     * info.id:直营商家id
     * @return DirectMoney
     */
	@ResponseBody
	@RequestMapping(value ="/getDirectMoney",produces = "text/json;charset=UTF-8")
	public String getDirectMoney(int directId){
		DirectMoney money = direct.getDirectMoney(directId);
		Gson gson = new Gson();
		return gson.toJson(money);
	}
	
    /**
     * 直营商家提现
     * drawing.merchantId :直营商家id
     * drawing.typeId : 4
     * @return
     */
	@ResponseBody
	@RequestMapping(value ="/directWithdraw",produces = "text/json;charset=UTF-8")
	public String directWithdraw(AgentDrawings drawing){
			String msg = sms.AgentWithdraw(drawing);
			Gson gson = new Gson();
			return gson.toJson(msg);
	}

	/**
	 * 根据订单id查询订单详情
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/getOrderFormDetailsByID",produces = "text/json;charset=UTF-8")
	public String getOrderFormDetailsByID(String orderFormId){
		List<OrderFormDetails> list = ofs.getOrderFormDetailsByOrderFormID(orderFormId);
		Gson gson = new Gson();
		return gson.toJson(list);
	}
	
    /**
     * 根据直营商家id查询订单
     * @param orderform
     * orderform.typeId:3 直营商家
     * orderform.status:订单状态 -1.未付款.0.取消,1.待发货 2.待收货,3.已完成,4.已评价,5.已退货(未处理)6.已退货(已处理),7.已投诉.8,操作完毕
     * orderform.superId:直营商家id
     * @param startTime
     * @param endTime
     * @param pageNum
     * @param pageCount
     * @return
     */
	@ResponseBody
	@RequestMapping(value ="/getOrderFormBySuperInfo",produces = "text/json;charset=UTF-8")
	public String getOrderFormBySuperInfo(OrderForm orderform,String startTime,String endTime,int pageNum,int pageCount){
		Map<Object, Object> map=ofs.getOrderFormBySuper(orderform,startTime,endTime,pageNum,pageCount);
		Gson gson = new Gson();
		return gson.toJson(map);
	}
	
	/**
	 * 直营后台发货
	 * @param trackingNum
	 * @param orderFormID
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/TheSupermarketDelivery",produces = "text/json;charset=UTF-8")
	public String TheSupermarketDelivery(String orderFormID,String trackingNum){
		return ofs.TheSupermarketDelivery(orderFormID,trackingNum);
	}
	/**
	 * 查询直营商家提现记录
	 * @param drawing
	 * drawing.merchantId:商家id
	 * drawing.typeId:直营商家类型 4
	 * drawing.status:状态
	 * @param pageNum
	 * @param pageCount
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getAgentDrawings", produces = "text/json;charset=UTF-8")
	public String getAgentDrawings(AgentDrawings drawing, int pageNum, int pageCount) {
		Map<Object, Object> map = sms.getAgentDrawings(drawing, pageNum, pageCount);
		Gson gson = new Gson();
		return gson.toJson(map);
	}
	
	/**
	 * 查询直营商家电话
	 * @param directId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getDirectPhone", produces = "text/json;charset=UTF-8")
	public String getDirectPhone(int directId){
		DirectInfo  info =direct.getDirectInfoById(directId);
		return info.getPhone();
	}
	/**
	 * 查询直营商家
	 * @param directId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getDirectInfoById", produces = "text/json;charset=UTF-8")
	public String getDirectInfoById(int directId){
		DirectInfo  info =direct.getDirectInfoById(directId);
		Gson gson = new Gson();
		return gson.toJson(info);
	}
	
	/**
	 * 根据商品id
	 * @param entrepotId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getDirectProductById", produces = "text/json;charset=UTF-8")
	public String getDirectProductById(int productId){
		DirectProduct en=direct.getDirectProductById(productId);
		DirectInfo info =direct.getDirectInfoById(en.getDirectId());
		List<ProductEvaluate> list =smes.getProductEvaluateByIdAndType(productId,3);
		Map<Object,Object> map=new HashMap<Object, Object>();
        Banner banner = direct.getBannerByProductId(productId);
		map.put("DirectProduct", en);
		map.put("Evaluates", list);
		map.put("banner", banner);
		map.put("DirectInfo", info);
		Gson gson = new Gson();
		return gson.toJson(map);
	}

}
